<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author LYS
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'plm:doc:pLMDocInfo:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsPlmDocPLMDocInfoForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { PLMDocInfo, pLMDocInfoSave, pLMDocInfoForm } from '/@/api/plm/doc/pLMDocInfo';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('plm.doc.pLMDocInfo');
  const { showMessage } = useMessage();
  const record = ref<PLMDocInfo>({} as PLMDocInfo);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增doc_008') : t('编辑doc_008'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('文档名称'),
      field: 'cdocumentname',
      component: 'Input',
      componentProps: {
        maxlength: 512,
      },
    },
    {
      label: t('修改时间'),
      field: 'cdmodifytime',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('修改次数'),
      field: 'cdmodifynum',
      component: 'Input',
      componentProps: {
        maxlength: 10,
      },
    },
    {
      label: t('格式'),
      field: 'cdformat',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('大小'),
      field: 'cdsize',
      component: 'Input',
      componentProps: {
        maxlength: 10,
      },
    },
    {
      label: t('页大小'),
      field: 'cdpagesize',
      component: 'Input',
      componentProps: {
        maxlength: 12,
      },
    },
    {
      label: t('摘要'),
      field: 'cddetail',
      component: 'Input',
      componentProps: {
        maxlength: 2000,
      },
    },
    {
      label: t('文档状态'),
      field: 'cdlock',
      component: 'Input',
      componentProps: {
        maxlength: 2,
      },
    },
    {
      label: t('关键字'),
      field: 'cdkeyword',
      component: 'Input',
      componentProps: {
        maxlength: 2000,
      },
    },
    {
      label: t('备注'),
      field: 'cdremark',
      component: 'Input',
      componentProps: {
        maxlength: 2000,
      },
    },
    {
      label: t('模板编号'),
      field: 'cdmodelid',
      component: 'Input',
      componentProps: {
        maxlength: 20,
      },
    },
    {
      label: t('分类ID'),
      field: 'cmkindid',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('流程ID'),
      field: 'cinstanceflowid',
      component: 'Input',
      componentProps: {
        maxlength: 20,
      },
    },
    {
      label: t('项目编号'),
      field: 'cprojectid',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('阶段编号'),
      field: 'cphaseid',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('任务编号'),
      field: 'cactiveid',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('产品编号'),
      field: 'cproduct',
      component: 'Input',
      componentProps: {
        maxlength: 20,
      },
    },
    {
      label: t('cprimarydocid'),
      field: 'cprimarydocid',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('cprimarydocver'),
      field: 'cprimarydocver',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('创建人'),
      field: 'ccreatorid',
      component: 'Input',
      componentProps: {
        maxlength: 20,
      },
    },
    {
      label: t('cfrezontime'),
      field: 'cfrezontime',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('cisencryption'),
      field: 'cisencryption',
      component: 'Input',
      componentProps: {
        maxlength: 1,
      },
    },
    {
      label: t('文档来源'),
      field: 'sourceid',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('文档密级'),
      field: 'secrecygradeid',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('生效时间'),
      field: 'validtime',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('失效时间'),
      field: 'trashtime',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('英文名称'),
      field: 'englishname',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('backup1'),
      field: 'backup1',
      component: 'Input',
      componentProps: {
        maxlength: 2,
      },
    },
    {
      label: t('backup2'),
      field: 'backup2',
      component: 'Input',
      componentProps: {
        maxlength: 2,
      },
    },
    {
      label: t('是否已打印'),
      field: 'isprint',
      component: 'Input',
      componentProps: {
        maxlength: 1,
      },
    },
    {
      label: t('文档发放'),
      field: 'issuance',
      component: 'Input',
      componentProps: {
        maxlength: 2,
      },
    },
    {
      label: t('文档类型'),
      field: 'sortid',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('ctimestamp'),
      field: 'ctimestamp',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('checkdoctimestamp'),
      field: 'checkdoctimestamp',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('ismainfile'),
      field: 'ismainfile',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('修改人'),
      field: 'cmodifier',
      component: 'Input',
      componentProps: {
        maxlength: 20,
      },
    },
    {
      label: t('创建时间'),
      field: 'createtime',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('修改次数'),
      field: 'modifynum',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('打印次数'),
      field: 'cdprintnum',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('cdwidth'),
      field: 'cdwidth',
      component: 'Input',
      componentProps: {
        maxlength: 20,
      },
    },
    {
      label: t('cdheight'),
      field: 'cdheight',
      component: 'Input',
      componentProps: {
        maxlength: 20,
      },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 12, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await pLMDocInfoForm(data);
    record.value = (res.pLMDocInfo || {}) as PLMDocInfo;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        cdocumentid: record.value.cdocumentid,
        cdocumentver: record.value.cdocumentver,
      };
      // console.log('submit', params, data, record);
      const res = await pLMDocInfoSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
